package org.op.wword;

import java.awt.Color;
import java.util.List;
import java.util.StringTokenizer;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

public class EditAliasDialogViewImpl implements EditAliasDialogView {

	private static final String TEXT_AREA_SEPARATOR = System.getProperty("line.separator");
	private List<String> aliases;
	private JTextArea aliasTextArea; // list of aliases as text
	private JOptionPane optionPane;
	
	private String message;
	private String title;
	
	public List<String> openAndGet(JFrame frame, List<String> aliasList) {
		// dialog shows a list of words in a box
		//   a button to delete the current selection from the box
		//   a line input to add to the box
		//   buttons for cancel and OK
		aliases = aliasList;
		String aliasText = getAliasText(aliases);
		
		aliasTextArea = new JTextArea(5,20);
		Border border = new CompoundBorder(BorderFactory.createLineBorder(Color.GRAY), new EmptyBorder(2,2,2,2));
		aliasTextArea.setBorder(border);
		aliasTextArea.setText(aliasText);
		
        Object[] body = {message, aliasTextArea};

        //Create the JOptionPane.
        optionPane = new JOptionPane(body,
                                    JOptionPane.QUESTION_MESSAGE,
                                    JOptionPane.OK_CANCEL_OPTION);
        
        JDialog dialog = optionPane.createDialog(frame, title);
        dialog.setVisible(true);
		
        Object selectedValue = optionPane.getValue();
        if (selectedValue.equals(JOptionPane.CANCEL_OPTION)) {
        	// do nothing
        } else {
        	// TODO - if there is an error, then print it out
        	refreshAliasList(aliasTextArea.getText());
        }
        
		return aliases;
	}

	/**
	 *   Takes the text area and creates a new list
	 *   
	 * @param text
	 */
	private void refreshAliasList(String text) {
		aliases.clear();
		StringTokenizer tokenizer = new StringTokenizer(text, TEXT_AREA_SEPARATOR);	
		while (tokenizer.hasMoreTokens()) {
			aliases.add(tokenizer.nextToken());
		}
	}

	/**
	 *   Creates text from a list
	 *   
	 * @param aliasList
	 * @return
	 */
	private String getAliasText(List<String> aliasList) {
		StringBuffer text = new StringBuffer("");
		boolean once = false;
		for (String alias : aliasList) {
			if (once) {
				text.append(TEXT_AREA_SEPARATOR);
			} else {
				once = true;
			}
			text.append(alias);
		}
		return text.toString();
	}

	public List<String> getAliases() {
		return aliases;
	}

	public void setAliases(List<String> aliases) {
		this.aliases = aliases;
	}

	public JTextArea getAliasTextArea() {
		return aliasTextArea;
	}

	public void setAliasTextArea(JTextArea aliasTextArea) {
		this.aliasTextArea = aliasTextArea;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}
